package com.algorithms;

public class BinaryTreeMap<T extends Comparable<T> , V> {
	
	private Node<T,V> node;

	public void put(T key,V value ){
		if(node == null){
			node = new Node<T,V>(key,value);
		}else{
			node.add(key, value);
		}
	}
	
	public  V get(T key){
		return node.find(key);
	}
	
	
	private static class Node<T extends Comparable<T> , V>{

		private T itsKey;
		private V itsValue;
		
		@SuppressWarnings("unchecked")
		private Node<T,V>[] nodes = new Node[2];
		
		private final static int LESS = 0;
		private final static int GREATER= 1;
		
		public Node(T key , V value) {
			this.itsKey = key;
			this.itsValue = value;
		} 
		
		public  void add(T key,V value ){
			if(itsKey.compareTo(key) == 0){
				itsValue = value;
			}else{
				int nodeIndex = selectSubNode(key);
				if(nodes[nodeIndex] == null){
					nodes[nodeIndex] = new Node<T,V>(key,value);
				}else{
					nodes[nodeIndex].add(key, value);
				}
				 
			}
		}
		
		public V find(T key){
			if(itsKey.compareTo(key) == 0){
				return itsValue;
			}
			int nodeIndex = selectSubNode(key);
			return nodes[nodeIndex].find(key);
		}
		
		private int selectSubNode(T key) {
			return itsKey.compareTo(key) <0 ? GREATER : LESS;
		}
	}
	
	public static void main(String[] args) {
		BinaryTreeMap<String, String> map = new BinaryTreeMap<String, String>();
		map.put("b", "1");
		map.put("a", "2");
		map.put("c", "3");
		System.out.println(map.get("c"));
	}
	
}
